package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.mall.MallZoneGoods;
import com.tencreat.mapper.mall.MallZoneGoodsMapper;
import com.tencreat.service.mall.IMallZoneGoodsService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专区商品对应业务Service业务层处理
 *
 * @author wangkai
 * @date 2021-11-22
 */
@Service
public class MallZoneGoodsServiceImpl extends ServiceImpl<MallZoneGoodsMapper, MallZoneGoods> implements IMallZoneGoodsService {


    /**
     * 查询专区商品对应业务
     *
     * @param id 专区商品对应业务ID
     * @return 专区商品对应业务
     */
    @Override
    public MallZoneGoods selectMallZoneGoodsById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询专区商品对应业务列表
     *
     * @param mallZoneGoods 专区商品对应业务
     * @return 专区商品对应业务
     */
    @Override
    public List<MallZoneGoods> selectMallZoneGoodsList(MallZoneGoods mallZoneGoods) {
        return baseMapper.selectList(Wrappers.query(mallZoneGoods));
    }

    /**
     * 新增专区商品对应业务
     *
     * @param mallZoneGoods 专区商品对应业务
     * @return 结果
     */
    @Override
    public int insertMallZoneGoods(MallZoneGoods mallZoneGoods) {
        Integer count = lambdaQuery().eq(MallZoneGoods::getZoneId, mallZoneGoods.getZoneId()).count();
        mallZoneGoods.setCreateTime(LocalDateTime.now());
        mallZoneGoods.setSort(Long.valueOf(count));
        return baseMapper.insert(mallZoneGoods);
    }

    /**
     * 修改专区商品对应业务
     *
     * @param mallZoneGoods 专区商品对应业务
     * @return 结果
     */
    @Override
    public int updateMallZoneGoods(MallZoneGoods mallZoneGoods) {
        mallZoneGoods.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallZoneGoods);
    }

    /**
     * 批量删除专区商品对应业务
     *
     * @param ids 需要删除的专区商品对应业务ID
     * @return 结果
     */
    @Override
    public int deleteMallZoneGoodsByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除专区商品对应业务信息
     *
     * @param id 专区商品对应业务ID
     * @return 结果
     */
    @Override
    public int deleteMallZoneGoodsById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public boolean reSortOrderReduce(MallZoneGoods mallZoneGoods) {
        List<MallZoneGoods> list = lambdaQuery().eq(MallZoneGoods::getZoneId, mallZoneGoods.getZoneId()).orderByAsc(MallZoneGoods::getSort).list();
        int indexOf = list.stream().map(MallZoneGoods::getId).collect(Collectors.toList()).indexOf(mallZoneGoods.getId());
        if (indexOf == 0) {
            return true;
        }
        MallZoneGoods orgZone = list.get(indexOf);
        MallZoneGoods oldZone = list.get(indexOf - 1);

        Long oldSortOrder = oldZone.getSort();
        Long orgSortOrder = orgZone.getSort();

        orgZone.setSort(oldSortOrder);
        oldZone.setSort(orgSortOrder);

        updateById(oldZone);
        updateById(orgZone);

        return true;
    }

    @Override
    public boolean reSortOrderPlus(MallZoneGoods mallZoneGoods) {
        List<MallZoneGoods> list = lambdaQuery().eq(MallZoneGoods::getZoneId, mallZoneGoods.getZoneId()).orderByAsc(MallZoneGoods::getSort).list();
        int indexOf = list.stream().map(MallZoneGoods::getId).collect(Collectors.toList()).indexOf(mallZoneGoods.getId());
        if (indexOf == list.size() - 1) {
            return true;
        }
        MallZoneGoods orgZone = list.get(indexOf);
        MallZoneGoods oldZone = list.get(indexOf + 1);

        Long oldSortOrder = oldZone.getSort();
        Long orgSortOrder = orgZone.getSort();

        orgZone.setSort(oldSortOrder);
        oldZone.setSort(orgSortOrder);

        updateById(oldZone);
        updateById(orgZone);

        return true;
    }

    @Override
    public IPage<MallZoneGoods> list2(Page<MallZoneGoods> page, MallZoneGoods mallZoneGoods) {
        if (mallZoneGoods.getSortDesc() != null && mallZoneGoods.getSortStr() != null){
            if (mallZoneGoods.getSortDesc().equals("descending")){
                mallZoneGoods.setSortDesc("desc");
            }else {
                mallZoneGoods.setSortDesc("asc");
            }
        }
        return baseMapper.list2(page,mallZoneGoods);
    }
}
